package controller;

import java.util.List;
import java.util.Vector;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.activiti.engine.runtime.Execution;
import org.activiti.engine.task.Task;

import wfm.activiti.ActivitiManager;
import wfm.entities.Ticket;
import wfm.entities.User;
import wfm.entities.WFInfo;
import wfm.entities.WorkerShowBean;
import wfm.query.QueryDao;

import com.constants.Constants;
import com.web.util.WebUtils;

@ManagedBean(name = "poolController")
@SessionScoped
public class PoolController {

	@ManagedProperty(value = "#{user}")
	private User user;
	
	@ManagedProperty(value = "#{wfInfo}")
	private WFInfo wfInfo;
	
	private String procId = null;
	private String page = "";
	private String instanceId = "";
	private String taskId = "";

	public String getProcessInfo(Task task) {
		ActivitiManager manager = ActivitiManager.getInstance();
		Object o = manager.getRuntimeService().getVariable(
				task.getExecutionId(), "test");

		if (o == null)
			return "";
		else
			return o.toString();
	}
	
	public void abortProcess (Task task){  	
		if(task!=null){
			QueryDao.abortProcess(task);
		}
			
    }

	public List<Task> groupWorkerTasks() {

		if (getUser() != null) {
			List<Task> tasks = QueryDao
					.getAllTasksWithCandidateGroupAndNoAssignee(getUser()
							.getUserGroup().toString());
			return tasks;
		}
		return null;
	}

	public List<Task> tasksFromUser() {
		if (getUser() != null) {
			List<Task> tasks = QueryDao.getAllTasksWithAssignee(getUser()
					.getName());
			return tasks;
		}
		return null;
	}
	
	public List<WorkerShowBean> tasksWorkerToFinish() {
		List<WorkerShowBean> workerBeans= new Vector<WorkerShowBean>(); 
		if (getUser() != null) {
			List<Execution> executions = QueryDao.getAllMessageSendingEvents(Constants.MESSAGE_WORK_DONE);
			
			for(Execution exec : executions){
				try { 
					Ticket ticket=(Ticket)QueryDao.getVariableToProcessById(exec.getProcessInstanceId(), "ticket");
					if(ticket!=null && ticket.getSelectedPartner()!=null && ticket.getSelectedPartner().getEmail()!=null && ticket.getSelectedPartner().getEmail().equals(user.getName())){
						WorkerShowBean bean=new WorkerShowBean(exec,ticket);
						workerBeans.add(bean);
					}
				} catch (Throwable t){
					//in cause of problem with non serializable objects
				}
			}
		}
		return workerBeans;
	}

	public void assignTaskToUserAction(Task task) {
		User user = null;
		
		if(this.getUser()==null){
			user = new User("b.mueller@realestate.com", "beate", "Beate",
				"Müller", User.GROUP_DESKTOPWORKER);
		}else{
			user = this.getUser();
		}

		if (task != null) {
			QueryDao.setAssigneeToTask(task, user.getName());
		}
	}

	public void unassignTask(Task task) {
		if (task != null) {
			QueryDao.unassignTask(task);
		}
	}

	public String handleTask(Task task) {
		if (task != null) {
			String currentActivity = QueryDao.getFormKey(task);
			wfInfo.setTaskId(task.getId());
			wfInfo.setInstanceId(task.getProcessInstanceId());
			
			System.out.println("Handle navigate to "+currentActivity);
			
			return WebUtils.redirect(currentActivity);	
		}
		
		return WebUtils.redirect("myTasks");
	}
	
	public String finishWork(Execution exec) {
		if (exec != null) {
			QueryDao.messageReceveived(Constants.MESSAGE_WORK_DONE, exec.getId());
			
			System.out.println("Work Finished");
			
			return WebUtils.redirect("workerTasks");
		}
		
		return WebUtils.redirect("workerTasks");
	}
	
	

	// to start a random process for the pool 
	public void startProcess() {
		ActivitiManager manager = ActivitiManager.getInstance();
		manager.deployProcess(Constants.DEPLOY_PROCESS);
		manager.startProcess();
	}
	
	public boolean showCreateTicket(){
		if(user.getUserGroup()!=null && user.getUserGroup().equals(User.GROUP_DESKTOPWORKER)){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean showPools(){
		if(user.getUserGroup()!=null && !user.getUserGroup().equals(User.GROUP_WORKER)){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean showWorkerTasks(){
		if(user.getUserGroup()!=null && user.getUserGroup().equals(User.GROUP_WORKER)){
			return true;
		}else{
			return false;
		}
	}
	
	public String getProcId() {
		return procId;
	}

	public void setProcId(String procId) {
		this.procId = procId;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public WFInfo getWfInfo() {
		return wfInfo;
	}

	public void setWfInfo(WFInfo wfInfo) {
		this.wfInfo = wfInfo;
	}

	public String getInstanceId() {
		return this.instanceId;
	}

	public void setInstanceId(String instanceId) {
		this.instanceId = instanceId;
	}

	public String getTaskId() {
		return this.taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

}